IF OBJECT_ID('ssf.trg_dependency_object') IS NOT NULL
	DROP TRIGGER ssf.trg_dependency_object
GO

CREATE TRIGGER trg_dependency_object
ON ssf.dpnd_dependency_object
FOR INSERT, UPDATE
AS
/*
<object_header>

<object_summary>
	<purpose><![CDATA[
Enforce uniqueness of ssf.dpnd_dependency_object.
Cannot use a unique constraint because the key is longer than the current maximum of 900 bytes.
	]]></purpose>
</object_summary>

<object_header>
*/

IF EXISTS (
	SELECT d.header_object_id
		, d.dependency_type
		, d.dpnd_object_name
	FROM inserted i
	JOIN ssf.dpnd_dependency_object d
	ON i.header_object_id = d.header_object_id
	AND i.dependency_type = d.dependency_type
	AND i.dpnd_object_name = d.dpnd_object_name
	GROUP BY d.header_object_id
		, d.dependency_type
		, d.dpnd_object_name
	HAVING COUNT(*) > 1
)
BEGIN
	RAISERROR('<dpnd_dependency_object> must be unique on (header_object_id, dependency_type, dpnd_object_name).', 16, 1)
	ROLLBACK TRANSACTION
END
GO
